# -*- coding: utf-8 -*-

import json


def build_json(code, description, data, token=''):
    return json.dumps({
        'isBase64Encoder': False,
        'statusCode': 200,
        'headers': {},
        'body': {
            'statusCode': code,
            'description': description,
            'data': data,
            'token': token
        }
    })


def parse_event(event):
    try:
        evt = json.loads(event)
        headers = evt['headers']
        body = eval(evt['body']) \
            if 'body' in evt and evt['body'] is not None and len(evt['body']) != 0 \
            else None
        query_parameters = evt['queryParameters'] \
            if 'queryParameters' in evt \
            else None
        path_parameters = evt['pathParameters'] \
            if 'pathParameters' in evt \
            else None
        deploy_config = headers['X-Deploy-Config'] \
            if 'X-Deploy-Config' in headers and headers['X-Deploy-Config'] is not None \
            else 'beta'
        client_token = headers['x-token'] \
            if 'x-token' in headers \
            else None
        return evt['httpMethod'], query_parameters, path_parameters, body, client_token, deploy_config
    except Exception:
        raise ValueError(1000005, 'Invalid Params')
